package com.bnyy.medicalHousekeeper.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bnyy.common.view.FormInfoItem;
import com.bnyy.medicalHousekeeper.PopupWindowHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.IncomeDetail;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/10
 **/
public class IncomeDetailActivity extends BaseActivityImpl {
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.form_source_of_income_1)
    FormInfoItem formSourceOfIncome1;
    @BindView(R.id.form_source_of_income_2)
    FormInfoItem formSourceOfIncome2;
    @BindView(R.id.form_source_of_income_3)
    FormInfoItem formSourceOfIncome3;
    @BindView(R.id.form_total)
    FormInfoItem formTotal;

    private HashMap<String, Object> params = new HashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_income_detail;
    }

    @Override
    public String getTitleStr() {
        return "收益详情";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tvYear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> options = new ArrayList<>();
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                for (int y = year; y >= 2019; y --) {
                    options.add(y + "");
                }
                PopupWindowHelper.getInstance(mContext).showSingleSelectPopupWindow(options, new PopupWindowHelper.SingleSelect.CallBackImpl() {
                    @Override
                    public void onSelect(String option, int position) {
                        super.onSelect(option, position);
                        if (!option.equals(tvYear.getText().toString())) {
                            tvYear.setText(option);
                            params.put("year", Integer.parseInt(option));
                            getIncomeDetail();
                        }
                    }
                });
            }
        });

        tvMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> options = new ArrayList<>();
                for (int m = 1; m <= 12; m++) {
                    options.add(m + "");
                }
                PopupWindowHelper.getInstance(mContext).showSingleSelectPopupWindow(options, new PopupWindowHelper.SingleSelect.CallBackImpl() {
                    @Override
                    public void onSelect(String option, int position) {
                        super.onSelect(option, position);
                        if (!option.equals(tvMonth.getText().toString())) {
                            tvMonth.setText(option);
                            params.put("month", Integer.parseInt(option));
                            getIncomeDetail();
                        }
                    }
                });
            }
        });


        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;

        tvYear.setText(year + "");
        tvMonth.setText(month + "");

        params = new HashMap<>();
        params.put("year", year);
        params.put("month", month);

        getIncomeDetail();
    }

    private void getIncomeDetail() {
        requestManager.request(requestManager.mJavaRetrofitService.getIncomeDetail(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<IncomeDetail>(mContext) {
                    @Override
                    public void onSuccess(IncomeDetail incomeDetail) {
                        super.onSuccess(incomeDetail);

                        formSourceOfIncome1.setContent(incomeDetail.getProfit() + "");
                        formSourceOfIncome2.setContent(incomeDetail.getDirect_user_profit() + "");
                        formSourceOfIncome3.setContent(incomeDetail.getNo_direct_user_profit() + "");
                        formTotal.setContent(incomeDetail.getTotal() + "");
                    }
                });
    }
}




























